﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace SAM.Configuration.Domain
{
    public class XMLConfigurationBuild:AbstractConfigurationBuild
    {
        public XMLConfigurationBuild() : base()
        {

        }
        public XMLConfigurationBuild(string baseFilePath) : base(baseFilePath)
        {

        }

        public override T Read<T>(string fileName)
        {
            string fullPath = this.FullPath(fileName);
            bool exists = this.ExistsFile(fullPath);
            if (!exists) return default(T);
            using (System.IO.StreamReader reader = new System.IO.StreamReader(fullPath))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(T));
                return (T)(serializer.Deserialize(reader));
            }
        }

        public override void Write<T>(string fileName, T data)
        {
            string fullPath = this.FullPath(fileName);
            if (data == null) return;
            using (System.IO.StreamWriter writer = new System.IO.StreamWriter(fullPath,false, Encoding.UTF8))
            {
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
                xmlSerializer.Serialize(writer, data);
            }
            
        }
    }
}
